<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <!-- <page-filter v-model:query="filterInfo.query" :filter-list="filterInfo.list" /> -->
    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="getQuartzLogPage"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="true"
      :check-box="false"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-executeResult="scope">
        <span v-for="(item, key) in successStatusList" :key="key">
          <span v-if="item.code == scope.row.executeResult">{{
            item.name
          }}</span>
        </span>
      </template>
    </dialog-table>
  </page-dialog>
</template>

<script setup name="quartzLog">
import { getQuartzLogPage } from '@/api/log.js'
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

const successStatusList = ref(
  proxy.$store.state.permission.dict['success_status']
)
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'begin_time desc',
    ...props.query,
  },
  // list: [
  //   {
  //     type: "input",
  //     label: "名称",
  //     value: "jobName",
  //     event: search
  //   },
  //   { type: "button", label: "搜索", event: search }
  // ]
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '任务名称', value: 'jobName' },
    { label: '任务分组', value: 'jobGroup' },
    { label: 'cron表达式', value: 'cronExpression' },
    { label: '类名', value: 'beanName' },
    { label: '方法名', value: 'methodName' },
    { label: '开始时间', value: 'beginTime' },
    { label: '结束时间', value: 'endTime' },
    { label: '耗时（秒）', value: 'costTime' },
    { label: '执行结果', value: 'executeResult', type: 'slot' },
    { label: '异常信息', value: 'exception' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: 'quartz日志',
  width: '85%',
  btLoading: false,
  btList: [{ label: '关闭', event: handleClose, show: true }],
})
const selectRow = ref('')

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
})

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  // 暂不清楚以下代码作用，但不注释btList[1]会报错
  // switch (event) {
  //   case 'tableClickRow':
  //     selectRow.value = data
  //     dialogInfo.value.btList[1].disabled = !selectRow.value
  //     break
  //   case 'tableCheck':
  //     if (data.length) {
  //       selectRow.value = data
  //       dialogInfo.value.btList[1].disabled = !selectRow.value
  //     }
  //     break
  // }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>
